<template>
  <div class="login">
    <div class="left">
      <div class="left-header">
        <img class="left-logo" src="@/assets/img/login_logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <!-- el-form表单 -->
      <el-form class="form" :model="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="8">
              <!-- 图形验证码 -->
              <img @click="updateCodeImg" class="code-img" :src="codeUrl" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            <span class="clause">我已阅读并同意</span>
            <el-link type="primary">用户协议</el-link>
            <span>和</span>
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click.stop="submit">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login_right.png" alt />
    <!-- 1.导入注册组件 2.注册组件 3.使用组件 -->
    <register ref="register"></register>
  </div>
</template>
<script>
// 1.导入注册组件
import register from "@/views/login/register.vue";
import { toLogin } from "@/api/register.js";
import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  // 2.注册组件
  components: {
    register
  },
  data() {
    return {
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "", //是	string	手机号
        password: "", //是	string	密码
        code: "", //是	string	验证码
        isPass: ""
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let _phone = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_phone.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号码"));
              }
            }
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "请输入6-12位密码", trigger: "change" }
        ],
        code: [
          {
            required: true,
            message: "请输入验证码号",
            trigger: ["change", "change"]
          },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" }
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value === true) {
                callback();
              } else {
                callback(new Error("请勾选协议"));
              }
            }
          }
        ]
      }
    };
  },
  methods: {
    // 点击注册按钮显示注册框
    showRegister() {
      // 再register标签上设置一个ref  ref='register'
      // 通过this.$refs.值 拿到子组件的this 或叫实例
      // 再通过子组件的实例修改它子组件里的数据  isShow
      this.$refs.register.isShow = true;
    },
    // 登录验证
    submit() {
      // 获取el-form组件的this
      this.$refs.form.validate(result => {
        if (result) {
          // 使用el 消息弹框
          // el将$message添加到了vue实例的原型中，
          // 所有我们可以直接通过vue实例访问$message方法
          // this.$message.success("验证通过");
          // 调用登录接口
          toLogin(this.form).then(res => {
            saveLocal(res.data.token);
            this.$message.success("登录成功");
            this.$router.push("/layout");
          });
          // 此时我们只用关系数据成功处理，
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    updateCodeImg() {
      this.codeUrl =
        process.env.VUE_APP_URL + "/captcha?type=login&aaax=" + Date.now();
    }
  },
  created() {
    if (getLocal()) {
      this.$router.push("/layout/chart");
    }
    this.$store.state.role = "超级管理员";
  }
};
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(to right, #1493fa, #01c6fa);
  display: flex;
  justify-content: space-around;
  align-items: center;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px;
    .left-header {
      display: flex;
      align-items: center;
      .left-logo {
        width: 22px;
        height: 17px;
        margin-right: 20px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin: 0 20px;
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 25px;
      .btn {
        width: 100%;
      }
      .code-img {
        width: 100%;
        height: 40px;
      }
      .clause {
        vertical-align: -1px;
      }
    }
  }
}
</style>